<?php
namespace apiadmin\modules\v1\controllers\finance;

use common\models\Balance;
use common\models\WithdrawLog;
use \Yii;
use apiadmin\modules\v1\controllers\CoreController;


use common\utils\OutputExecl;
/*
	会员提现相关控制器
*/
class WithdrawController extends CoreController
{

	//提现列表
	public function actionWithdraw_list()
	{
		$params = array(
			'page'	=> $this->request('page',1),
			'limit'	=> $this->request('page_size'),
			'order'	=> 'id desc'
		);
		$whereArr = $this->formatWhere();
		$list = WithdrawLog::withdrawList($whereArr,$params,['collect']);
		$pages = WithdrawLog::$pages;
		$typeName = Balance::balanceTypeName();
		$withdrawState = Yii::$app->params['withdrawState'];

		//汇总
       $total = WithdrawLog::find()->where(['state'=>4])->sum('money');
		$chargeTotal = WithdrawLog::find()->where(['state'=>4])->sum('fee');

		$extend = [
			'pages'=>$pages,
			'type_name'=>$typeName,
			'withdrawState'=>$withdrawState,
			'total'=>$total,
			'chargeTotal' => $chargeTotal,
			'where' => $whereArr,
		];

		$this->out('提现列表',$list,$extend);
	}

	private function formatWhere()
	{
		$whereArr = $where = $and = [];
		$search = json_decode($this->request('search'),1);

		if(isset($search['member_id']) && $search['member_id'])
			$where['member_id'] = $search['member_id'];

		if(isset($search['state']) && $search['state'])
			$where['state'] = $search['state'];	

		$date = $search['date'];
		if($date && $date['0'] && $date['1'])
			$and[] = ['between', 'create_time', strtotime($date[0]),strtotime($date[1])]; 				

		$whereArr['where'] = $where;
		$whereArr['and'] = $and;
		return $whereArr;
	}


	/*
		提现审核
		* id
		* status 2通过 3驳回  
	*/
	public function actionWithdraw_check()
	{
		if(!$id = $this->request('id')) $this->error('参数有误');
		if(!$status = $this->request('status')) $this->error('参数有误');

		try
		{
			$transaction = Yii::$app->db->beginTransaction();
			$data = ['state'=>$status,'update_time'=>time()];
			$where = ['id'=>$id];

			//更新状态
			if(!WithdrawLog::updateAll($data,$where)) throw new \Exception("操作失败");			
			if($status=='3')
			{
				//驳回申请 退回余额
				$log = WithdrawLog::getWithdrawLog($id);
				$res = Balance::addBalance($log['member_id'],$log['b_type'],$log['money'],21,$log['id'],'提现驳回',true);
				if(!$res['state']) throw new \Exception($res['msg']);
			}
			$transaction->commit();
			$this->out('操作成功');
		}catch(\Exception $e)
		{
			$transaction->rollback();
			$this->error($e->getMessage());
		}

	}

	//导出数据
	public function actionExport()
	{
		$whereArr  = $this->formatWhere();
		$params = array(
			'order' => 'id desc',
			'page'	=> $this->request('page','1'),
			'limit' => $this->request('page_size',1000),
		);
		$list = WithdrawLog::withdrawList($whereArr,$params,['collect']);

		//组织导出数据
		$exportData = array();
		foreach($list as $val)
		{

			$temp = [];
			$temp[] = $val['member_id'];
			$temp[] = $val['name'];
			$temp[] = $val['member_mobile'];
			$temp[] = $val['total_money'];
			$temp[] = $val['money'];
			$temp[] = $val['charge'];
			$temp[] = $val['bank_name'].'('.$val['bank_branch'].')';
			$temp[] = '卡号：'.$val['bank_num'];

			$temp[] = Yii::$app->params['withdrawText'][$val['status']];
			$temp[] = date("Y-m-d H:i:s",$val['create_time']);
			$exportData[] = $temp;
		}
		$headData = array('A1'=>'会员ID','B1'=>'姓名','C1'=>'电话','D1'=>'申请金额','E1'=>'实际金额','F1'=>'手续费','G1'=>'银行名称','H1'=>'卡号','I1'=>'状态','J1'=>'申请时间');
		$fileName = 'withdraw-'.date('Y-m-d').'.xls';
		$execlObj = new OutputExecl();
		$res = $execlObj->output($headData,$exportData,$fileName);
		if($res)
			$this->out('下载地址',array('url'=>$res));
		else
			$this->error('导出失败');
	}




}